package com.suishoupai.xmb.Security;

import org.springframework.http.HttpStatus;
import org.springframework.security.web.DefaultRedirectStrategy;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by caojian on 2017/4/18.
 */
public class AjaxRedirectStrategy extends DefaultRedirectStrategy {
    @Override
    public void sendRedirect(HttpServletRequest request, HttpServletResponse response, String s) throws IOException {
        String xrw = request.getHeader("X-Requested-With");
        if (xrw != null && xrw.contains("XMLHttpRequest")) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Denied");//对于ajax请求不重定向  而是返回错误代码
        } else {
//            super.sendRedirect(httpServletRequest, httpServletResponse, s);
            response.setStatus(HttpStatus.SEE_OTHER.value());
            response.setHeader("Location", s);
        }
    }
}
